define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'adminlte', 'template', 'formSelects-v4.min'], function ($, undefined, Backend, Table, Form, Adminlte, Template, formSelects) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'classes/index',
                    add_url: 'classes/add',
                    edit_url: 'classes/edit',
                    del_url: 'classes/del',
                    multi_url: 'classes/multi',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                sortName: 'id',
                columns: [
                    [
                        {field: 'state', checkbox: true, },
                        {
                            field: 'school.id',
                            title: __('学校'),
                            align: 'left',
                            searchList: $.getJSON('school/searchList'),
                            visible: false
                        },
                        {
                            field: 'grade_id',
                            title: __('年级'),
                            align: 'left',
                            searchList: $.getJSON('grade/searchList'),
                            visible: false
                        },
                        {field: 'logo', title: __('班徽'), operate: false, formatter: Table.api.formatter.image},
                        {field: 'name', title: __('Name'), operate: false},
                        {field: 'grade_name', title: __('Grade'), operate: false},
                        {field: 'school_name', title: __('School'), operate: false},
                        {field: 'garden_name', title: __('Garden'), operate: false},
                        {field: 'building_name', title: __('Building'), operate: false},
                        {field: 'classroom_name', title: __('Classroom'), operate: false},
                        {field: 'type_text', title: __('班级类型'), operate: false},
                        {field: 'student_count', title: __('Student Count'), operate: false, formatter: Controller.api.formatter.student_count},
                        {field: 'qrcode', title: __('二维码'), operate: false, formatter: Controller.api.formatter.qrcode},
                        {
                            field: 'created_at',
                            title: __('Create time'),
                            sortable: true,
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange'
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                commonSearch: true,
                searchFormVisible: true,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            // 监听下拉列表改变的事件
            $(document).on('change', 'select[name=school_id]', function () {
                $("#school_id").val($(this).val());
            });

            $(document).on('click', '.student', function () {
                if ($(this).data('type') == 1) {
                    Fast.api.open('classes/detail?class_id=' + $(this).data('id'), '详细信息', {area:['70%','70%']});
                } else {
                    Fast.api.open('classes/choose?class_id=' + $(this).data('id'), '详细信息', {area:['70%','70%']});
                }
            });


            $(document).delegate( ".qrcode", "click",function () {
                Backend.api.open('', '二维码', {
                    type: 1,
                    title: false,
                    closeBtn: 1,
                    area: '516px',
                    skin: 'layui-layer-nobg', //没有背景色
                    shadeClose: true,
                    content: $('#qrcode_'+$(this).data('id'))
                });
            });

        },
        add: function () {

            $(document).on('change', '.school', function () {
                Controller.api.initHeadMaster($(this).val());
            });


            Controller.api.bindevent();
        },
        edit: function () {

            Controller.api.initHeadMaster(Config.admin.school_id);

            $(document).on('change', '.school', function () {
                Controller.api.initHeadMaster($(this).val());
            });

            Controller.api.bindevent();
        },
        detail: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'student/index',
                    add_url: 'student/add',
                    edit_url: 'student/edit',
                    del_url: 'student/del',
                    multi_url: 'student/multi',
                }
            });

            var table = $("#table_detail");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url + '?class_id=' + Backend.api.query('class_id'),
                sortName: 'id',
                columns: [
                    [
                        {field: 'state', checkbox: true, },
                        {field: 'number', title: __('Number'),  operate: 'LIKE %...%',},
                        {field: 'edu_id_number', title: __('Edu Id Number'),  operate: 'LIKE %...%',},
                        {field: 'avatar', title: __('Avatar'), operate: false, formatter: Table.api.formatter.image},
                        {field: 'name', title: __('Name'), operate: 'LIKE %...%',},
                        {field: 'gender_text', title: __('Gender'), operate: false},
                        {field: 'birthday', title: __('Birthday'), operate: false},
                        {field: 'school.name', title: __('School'), operate: false,},
                        {field: 'grade.name', title: __('Grade'), operate: false,},
                        {field: 'classes.name', title: __('Classes'), operate: false,},
                        {field: 'parents.name', title: __('Parents Name'), operate: false,},
                        {field: 'parents.mobile', title: __('Parents Mobile'), operate: false,},
                        {
                            field: 'created_at',
                            title: __('Create time'),
                            sortable: true,
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange'
                        },
                        {field: 'status', title: __('Status'), align: 'center', formatter: Table.api.formatter.toggle, operate: false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                commonSearch: false,
                searchFormVisible: false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

        },
        choose: function() {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'classstudent/index',
                    add_url: 'classes/chooseAdd' + '?class_id=' + Config.admin.class_id,
                    del_url: 'classstudent/del',
                }
            });

            var table = $("#table_choose");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url + '?class_id=' + Config.admin.class_id,
                sortName: 'student.id',
                columns: [
                    [
                        {field: 'state', checkbox: true, },
                        {field: 'number', title: __('Number'),  operate: 'LIKE %...%',},
                        {field: 'edu_id_number', title: __('Edu Id Number'),  operate: 'LIKE %...%',},
                        {field: 'avatar', title: __('Avatar'), operate: false, formatter: Table.api.formatter.image},
                        {field: 'name', title: __('Name'), operate: 'LIKE %...%',},
                        {field: 'gender_text', title: __('Gender'), operate: false},
                        {field: 'parents.name', title: __('Parents Name'), operate: false,},
                        {field: 'parents.mobile', title: __('Parents Mobile'), operate: false,},
                        {
                            field: 'created_at',
                            title: __('Create time'),
                            sortable: true,
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange'
                        },
                    ]
                ],
                commonSearch: false,
                searchFormVisible: false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        chooseadd: function() {
            
            $(document).on('change', '.classes', function () {
                $.ajax({
                    url: "classes/students",
                    type: "POST",
                    dataType: "json",
                    data:{id:$(this).val(),class_id:$('.class_id').val()},
                    success: function (ret) {
                        $('#student_list').html(Template('student_tpl', ret));
                    }
                })
            });

            // 全选
            $(document).on('click', '#check_all', function () {
                if(this.checked){
                    $(".student").prop("checked",true);
                }else{
                    $(".student").prop("checked",false);
                }
            });

            
            Form.api.bindevent($("form#choose"), function (data, ret) {
                $("#table_choose").bootstrapTable('refresh');
                //$("#table").bootstrapTable('refresh');
            });

        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                student_count: function (value, row, index) {
                    return '<a class="student" data-id="'+row.id+'" data-type="'+row.type+'" href="javascript:;" >' + value + '人</a>';
                },
                qrcode: function (value, row, index) {
                    return "<img id='qrcode_"+row.id+"' style='width:100%;display:none;' src='" + row.qrcode + "'><a class='qrcode' href='javascript:;' style='max-width: 50px; max-height: 50px; display:inline-block;' data-id='" + row.id + "'><img style='width:100%;' src='" + row.qrcode + "'></a>";
                }
            },
            initHeadMaster : function (school_id) {
                $.ajax({
                    type: "POST",
                    url: "teacher/cxSelect",
                    data: {school_id:school_id},
                    dataType: "json",
                    success: function(ret){
                        $(".teacher").html(Template("tpl", ret));
                        formSelects.render('headmaster');
                    }
                });
            }
        }
    };
    return Controller;
});